package cn.hl.hm.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.constants.OrderStatus;
import cn.hl.hm.constants.SourceStatus;
import cn.hl.hm.entity.Coupon;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverRoute;
import cn.hl.hm.entity.Order;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.User;
import cn.hl.hm.service.DriverRouteService;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.OrderUtil;
import cn.hl.hm.util.Result;
import smsClient.TestSDKClient;

@Controller
@RequestMapping("/driverRouteController")
public class DriverRouteController {
	private static final Logger logger = LoggerFactory.getLogger(DriverController.class);
	private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	DriverRouteService driverRouteService;
	@Autowired
	DriverService driverService;
     
	/**
	 * @Title: login @Description:路线列表 @date: 2017年1月4日 @author: 星矢 @param
	 *         DriverAccount @param pass @param platformId @param request @param
	 *         response @throws IOException void @throws
	 */
	@RequestMapping(value = "/selectDriverRouteList", method = RequestMethod.POST)
	public String selectDriverRouteList(@RequestParam Integer driverId, @RequestParam Integer pageId,
			@RequestParam Integer pageCount, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		logger.info("司机路线业务/司机管理---获取司机线路列表/固定线路列表---driverRouteController/selectDriverRouteList");
		request.setCharacterEncoding("UTF-8");
		AppPager handle = AppPager.handle(pageId, pageCount);
		DriverRoute record = new DriverRoute();
		record.setDriverId(driverId);

		try {
			record.setPageId(handle.getPageId());
			record.setPageCount(handle.getPageCount());
			record.setRouteStatus("0");
			List<DriverRoute> driverRoute = driverRouteService.selectDriverRoute(record);
			if (null != driverRoute && driverRoute.size() > 0) {
				Result.ok(driverRoute, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;

			}
		} catch (Exception e) {
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
 
	/**
	 * @Title: send @Description:添加线路 @date: 2017年1月5日 @author: 星矢 @param
	 *         appId @param order @param request @param response @return
	 *         String @throws
	 */
	@RequestMapping(value = "/addDriverRoute", method = RequestMethod.POST)
	public String addDriverRoute(String appId, DriverRoute driverRoute, @RequestParam String source, Integer operId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("司机路线业务---添加司机线路---driverRouteController/addDriverRoute");
		if (source.equals(SourceStatus.BACKGROUND.getStatus()) && operId == null) {
			Result.ok(null, InfoCode.PARAM_ERROR, response);
			return null;
		}
		try {
			driverRoute.setCreateTime(new Date());
			driverRoute.setRouteStatus("0");
			int add = driverRouteService.addDriverRoute(driverRoute);
			if (add > 0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}

	/**
	 * @Title: send @Description:删除线路 @date: 2017年1月5日 @author: 星矢 @param
	 *         appId @param order @param request @param response @return
	 *         String @throws
	 */
	@RequestMapping(value = "/deleteRoute", method = RequestMethod.POST)
	public String addDriverRoute(String appId, @RequestParam Integer driverRouteID, @RequestParam String source,
			Integer operId, HttpServletRequest request, HttpServletResponse response) {
		logger.info("司机路线业务---删除司机线路---driverRouteController/deleteRoute");
		if (source.equals(SourceStatus.BACKGROUND.getStatus()) && operId == null) {
			Result.ok(null, InfoCode.PARAM_ERROR, response);
			return null;
		}
		try {
			DriverRoute driverRoute = new DriverRoute();
			driverRoute.setId(driverRouteID);
			driverRoute.setRouteStatus("1");
			// driverRoute.setCreateTime(new Date());
			int delete = driverRouteService.updateDriverRoute(driverRoute);
			if (delete > 0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}

	/**
	 * @Title: send @Description:修改司机线路 @date: 2017年1月5日 @author: 星矢 @param
	 *         appId @param order @param request @param response @return
	 *         String @throws
	 */
	@RequestMapping(value = "/updateRoute", method = RequestMethod.POST)
	public String updateRoute(String appId, @RequestParam Integer driverRouteId, DriverRoute driverRoute,
			@RequestParam String source, Integer operId, HttpServletRequest request, HttpServletResponse response) {
		logger.info("司机路线业务---修改司机线路信息---driverRouteController/updateRoute");
		if (source.equals(SourceStatus.BACKGROUND.getStatus()) && operId == null) {
			Result.ok(null, InfoCode.PARAM_ERROR, response);
			return null;
		}
		try {

			driverRoute.setId(driverRouteId);

			// driverRoute.setCreateTime(new Date());
			int update = driverRouteService.updateDriverRoute(driverRoute);
			if (update > 0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
}
